﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using TexturedQuad;

namespace kinect_music_shooter.GameWorld
{
    class Shot : GameObject
    {
        Quad quad;
        VertexDeclaration vertexDeclaration;
        Texture2D texture;
        BasicEffect quadEffect;
        GraphicsDevice graphicsDevice;


        public Shot(GraphicsDevice pGraphicsDevice, BasicEffect effect)
        {
            graphicsDevice = pGraphicsDevice;
            ////////////////////////////////////////////
            velocity = new Vector3(0, 0, 0);
            scale = 1.0f;
            boundingSphere.Center = position;
            boundingSphere.Radius = 2.5f;
            //////////////////////////////////////////////

            quad = new Quad(Vector3.Zero, Vector3.Forward, Vector3.Up, 6, 6);
            // Create a new SpriteBatch, which can be used to draw textures.
            //spriteBatch = new SpriteBatch(graphicsDevice);
            //texture = Content.Load<Texture2D>("Glass");
            quadEffect = effect; 

            vertexDeclaration = new VertexDeclaration(new VertexElement[]
                {
                    new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
                    new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
                    new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
                }
            );

            
        }

        public void LoadTexture(Texture2D pTexture)
        {
            texture = pTexture;

        }

        public void Update()
        {
            double length = Math.Sqrt(Math.Pow(position.X, 2) + Math.Pow(position.Y, 2));

            if ( length > 10)
            {
                float speed = velocity.Length();
                velocity.X = 0;
                velocity.Y = 0;
                velocity.Normalize();
                velocity *= speed;

            }

            if (length > 20 && KinectMusicShooterGame.HitTheWall)
            {
                velocity.X = -velocity.X;
                velocity.Y = -velocity.Y;
            }
            rotation.Z += 0.02f;
            position += velocity;
            boundingSphere.Center = position;
        }

        public void Draw2(Camera camera)
        {

            //quadEffect = new BasicEffect(graphicsDevice); ;
            //quadEffect.EnableDefaultLighting();
            quadEffect.World =
                        Matrix.CreateFromYawPitchRoll(
                        rotation.Y,
                        rotation.X,
                        rotation.Z) *

                        Matrix.CreateScale(scale) *

                        Matrix.CreateTranslation(position);
            quadEffect.View = camera.View;
            quadEffect.Projection = camera.Projection;
            quadEffect.TextureEnabled = true;
            quadEffect.Texture = texture;

            //graphicsDevice.Clear(Color.CornflowerBlue);

            foreach (EffectPass pass in quadEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphicsDevice.DrawUserIndexedPrimitives
                    <VertexPositionNormalTexture>(
                    PrimitiveType.TriangleList,
                    quad.Vertices, 0, 4,
                    quad.Indexes, 0, 2);
            }

        }
    }
}
